package com.zh.system.crawler.Pipeline;

import com.zh.web.domain.media.entity.Article;
import com.zh.web.domain.media.entity.Video;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Map;

@Slf4j
@Component
public class BiliPipeline implements Pipeline {

    private final AmqpTemplate amqpTemplate;

    public BiliPipeline(AmqpTemplate amqpTemplate) {
        this.amqpTemplate = amqpTemplate;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> map = resultItems.getAll();
        if (map.isEmpty()) {
            return;
        }
        map.forEach((k, v) -> {
            if (v instanceof Article) {
                log.info("发送消息给 article:{}", v);
                amqpTemplate.convertAndSend("ls.media.exchange", "article", v);
            } else if (v instanceof Video) {
                log.info("发送消息给 video:{}", v);
                amqpTemplate.convertAndSend("ls.media.exchange", "video", v);
            }
        });
    }
}
